<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ThinkYXC-CMS系统-文章列表</title>
    <link rel="stylesheet" type="text/css" href="../layui/css/layui.css">
    <link rel="stylesheet" type="text/css" href="../css/style.css">
    <style>
        .layui-btn+.layui-btn{
            margin-left: 10px !important;
        }
        .layui-btn.layuiadmin-btn-list{
            height: 38px;
            padding: 0px 18px;
            font-size: 14px;
            line-height: 38px;
        }
        .serchform{
            width:300px;
            padding: 13px 0px 63px 0px;
        }
    </style>
</head>

<body>
    
    <div class="layui-card">
  <!--<div class="layui-card-header">卡片面板</div>-->
  <div class="layui-card-body">
      <div style="padding: 10px;" class="demoTable">
          <button class="layui-btn layuiadmin-btn-list" data-type="getCheckData" lay-event="delet" id="delet">批量删除</button>
          <button class="layui-btn layuiadmin-btn-list" data-type="add" lay-event="add" id="add">添加文章</button>
          <button class="layui-btn layuiadmin-btn-list" data-type="updatas" lay-event="updatas" id="updatas">刷新</button>
        </div>
        
      <form class="layui-form serchform"  action="" method="post"  lay-filter="nei1s" id="nei1s">
                <div class="layui-form-item" style="position: absolute;">
            <label class="layui-form-label" style="width:33px"><span class="bitian"></span>分类</label>
            <div class="layui-input-block" style="margin-left:70px">
              <select name="forum" lay-filter="forum">
                  <option value="">请选择</option>
                 {foreach $forum as $i=>$value}
                     <option value="{$value.fid}"><?php echo str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;├&nbsp;",$value["level"])?>{$value.name}</option>
                {/foreach}
              </select>
            </div>
          </div>
         <div class="layui-form-item" style="left: 290px;position: absolute;">
            <label class="layui-form-label"><span class="bitian"></span>发布时间</label>
            <div class="layui-input-block">
              <input type="text" class="layui-input" id="time" name="time" placeholder="日期范围">
            </div>
        
          </div>
          
          <div class="layui-form-item" style="left: 560px;position: absolute;">
            <label class="layui-form-label"><span class="bitian"></span>关键词</label>
            <div class="layui-input-block">
              <input type="text" class="layui-input" id="serchtitle" name="serchtitle" placeholder="关键词">
            </div>
        
          </div>
          <button type="submit" class="layui-btn" lay-submit="" lay-filter="nei1" style="left: 900px;position: absolute;">搜索文章</button>
        </form>
        
    <table class="layui-hide" id="test" lay-filter="test"></table>
 
<script type="text/html" id="barDemos">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="statusp">

{{#  if(d.status == 1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="status">已发布</a>
  {{#  } else { }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="status">未发布</a>
  {{#  } }}
</script>
<script type="text/html" id="thumbnail">

{{#  if(d.thumbnail){ }}
    <a class="layui-btn layui-btn-xs" lay-event="thumbnail">有</a>
  {{#  } else { }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="thumbnail">无</a>
  {{#  } }}
</script>
  </div>
</div>
    
    
    
     <script type="text/javascript" src="../layui/layui.all.js"></script>   
<script>
var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,$ = layui.jquery
  ,laydate = layui.laydate
  ,table = layui.table;
  var util = layui.util,
  active = {
    getCheckData: function(){ //获取选中数据
      var checkStatus = table.checkStatus('test')
      ,data = checkStatus.data;
      if(!data[0]){
          layer.msg('请选中要删除的内容');
          return
      }
      layer.confirm('真的删除么？', function(index){
                //do something
        //   layer.alert(JSON.stringify(data));
              $.ajax({
                  url:"{:url('cms/dellist')}",
                  data:{'data':JSON.stringify(data)},
                  type:"Post",
                  dataType:"json",
                  success:function(data){
                    console.log(data);
                    if(data.code == 1){
                         updata();
                     }
                      layer.msg(data.msg);
                    
                  },
                  error:function(data){
                      $.messager.alert('错误',data.msg);
                  }
              });    
        
        layer.close(index);
}); 
      
    }
    ,getCheckLength: function(){ //获取选中数目
      var checkStatus = table.checkStatus('test')
      ,data = checkStatus.data;
      layer.msg('选中了：'+ data.length + ' 个');
    }
    ,isAll: function(){ //验证是否全选
      var checkStatus = table.checkStatus('test');
      layer.msg(checkStatus.isAll ? '全选': '未全选')
    }
  };
  
  $('.demoTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
   //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
  //详见：https://www.layui.com/doc/modules/table.html#totalRow
  table.render({
    elem: '#test'
    ,url:'/admin/cms/lists'
    ,toolbar: '#toolbarDemo'
    ,title: '文章数据'
    ,totalRow: true
    ,cols: [[
      {type: 'checkbox', fixed: 'left',totalRowText: '合计',}
      ,{field:'tid', title:'文章ID', sort: true, totalRow: true, align:'center'}
      ,{field:'subject', title:'标题', sort: true, align:'center'}
    //   ,{field:'email', title:'分类', edit: 'text', templet: function(res){
    //     return '<em>'+ res.email +'</em>'
    //   }}
      ,{field:'views', title:'点击量', sort: true, totalRow: true, align:'center'}
      ,{field:'username', title:'作者', sort: true, align:'center'}
      ,{field:'likes', title:'点赞量', sort: true, totalRow: true, align:'center'}
      ,{field:'create_dateup', title:'更新时间', align:'center',templet: function(d){
        return util.toDateString(d.create_dateup*1000)
      }}
      ,{field:'create_date', title:'发布时间', align:'center',templet: function(d){
        return util.toDateString(d.create_date*1000)
      }}
      ,{field:'thumbnail', title:'缩略图',toolbar: '#thumbnail', align:'center'}
      ,{field:'status', title:'发布状态',toolbar: '#statusp', align:'center'}
      ,{fixed: 'operate', title:'操作', toolbar: '#barDemos', align:'center',fixed: 'right' }
    ]]
    ,page: true
     ,response:{
		      statusName:'code', //规定返回的状态码字段为code
			  statusCode:1 //规定成功的状态码味200
    }
    ,parseData: function(res){ //res 即为原始返回的数据
    return {
      "code": res.code, //解析接口状态
      "msg": res.msg, //解析提示文本
      "count": res.count, //解析数据长度
      "data": res.data  //解析数据列表
    };
  }
    ,done: function(res, curr, count){
    //如果是异步请求数据方式，res即为你接口返回的信息。
    //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
    console.log(res.code);
    
    //得到当前页码
    console.log(curr); 
    
    //得到数据总量
    console.log(count);
  }
  });
  
  //工具栏事件
//   table.on('toolbar(test)', function(obj){
//     var checkStatus = table.checkStatus(obj.config.id);
//     switch(obj.event){
//       case 'getCheckData':
//         var data = checkStatus.data;
//         layer.alert(JSON.stringify(data));
//       break;
//       case 'getCheckLength':
//         var data = checkStatus.data;
//         layer.msg('选中了：'+ data.length + ' 个');
//       break;
//       case 'isAll':
//         layer.msg(checkStatus.isAll ? '全选': '未全选')
//       break;
//       case 'delet':
//           layer.msg('删除')
//       break;
//       case 'add':
//           layer.msg('添加')
//       break;
//       case 'edit':
//           layer.msg('编辑')
//       break;
//       case 'status':
//           layer.msg('状态')
//       break;
//     };
//   });
  
  //监听工具条 
table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
  var data = obj.data; //获得当前行数据
  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
  var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
    console.log(data)
 
  if(layEvent === 'detail'){ //查看
    //do somehing
    
    
  } else if(layEvent === 'del'){ //删除
    layer.confirm('真的删除么？', function(index){
      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
      layer.close(index);
      //向服务端发送删除指令
       $.ajax({
          url:"{:url('cms/del')}",
          data:{'tid':data.tid},
          type:"Post",
          dataType:"json",
          success:function(data){
            console.log(data);
            if(data.code == 1){
                //  updata();
             }
              layer.msg(data.msg);
            
          },
          error:function(data){
              $.messager.alert('错误',data.msg);
          }
      });
      
    });
  } else if(layEvent === 'edit'){ //编辑
    //do something
            layer.open({
  type: 2,
  area: ['100%', '100%'],
  content: '/admin/edit?tid='+data.tid,
  zIndex: layer.zIndex, //重点1
  success: function(layero){
    layer.setTop(layero); //重点2
  } ,cancel: function(){ 
    //右上角关闭回调
    updata();
    //return false 开启该代码可禁止点击该按钮关闭
  }
}); 
    
  } else if(layEvent === 'status'){ //编辑
    //do something
    
        //未发布改为已发布
        //已发布改为未发布
       $.ajax({
          url:"{:url('cms/status')}",
          data:{'tid':data.tid,'status':data.status},
          type:"Post",
          dataType:"json",
          success:function(data){
            console.log(data.code);
             if(data.code == 1){
                 updata();
             }
              layer.msg(data.msg);
              
          },
          error:function(data){
              $.messager.alert('错误',data.msg);
          }
      });
        

    
    
  } else if(layEvent === 'LAYTABLE_TIPS'){
      
    layer.alert('Hi，头部工具栏扩展的右侧图标。');
  }
});

    //重新渲染数据
    function updata(){
        table.reload('test', {
                  url: '/admin/cms/lists'
        });
        
    }
    
    //批量删除按钮事件
    $("#delet").on('click',function(){
        // layer.alert('dd');
        
        
    });
    
    var dates = '';
    var endates = '';
   //时间选择
   laydate.render({
    elem: '#time'
    // type: 'datetime'
    ,range: true
    // ,value: new Date()
    ,max: 0
    ,isInitValue: true
    ,done: function(value, date, endDate){
    console.log(value); //得到日期生成的值，如：2017-08-18
    console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
    console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
    console.log(endDate.year+'-'+endDate.month+'-'+endDate.date);
     dates = date.year+'-'+date.month+'-'+date.date;
     endates = endDate.year+'-'+endDate.month+'-'+endDate.date;
     
    
  }
  });
  
    //监听提交
  form.on('submit(nei1)', function(data){
    //     var data = form.val('example');
    // alert(JSON.stringify(data));
    
    // console.log(JSON.stringify(data));
    var dast = form.val("nei1s");
    console.log(dast)
    if(!dast.serchtitle){
            dast.serchtitle = '';
        }
        if(!dast.forum){
            dast.forum = '';
        }
        if(!dates||dates == 'undefined-undefined-undefined'){
            dates = '';
        }
        if(!endates||endates == 'undefined-undefined-undefined'){
            endates = '';
        }
    
    table.reload('test', {
            url: '/admin/cms/lists?time='+dates+'&ovtime='+endates+'&serchtitle='+dast.serchtitle+'&forum='+dast.forum
        });
    
     return false;
  })
    
    //添加按钮事件
    $("#add").on('click',function(){
        // layer.alert('dds');
        layer.open({
  type: 2,
  area: ['100%', '100%'],
  content: '/admin/addcms',
  zIndex: layer.zIndex, //重点1
  success: function(layero){
    layer.setTop(layero); //重点2
  },
  cancel: function(){
     //右上角关闭回调
    updata();
    //return false 开启该代码可禁止点击该按钮关闭
    }  
});   
    });
    
    //刷新数据按钮事件
    $("#updatas").on('click',function(){
        updata();
    });
 
   </script>
</body>
</html>